<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>VariableConfig 前端Demo</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .container {
            background: white;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
            overflow: hidden;
            width: 100%;
            max-width: 800px;
        }

        .header {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            color: white;
            padding: 20px;
            text-align: center;
        }

        .header h1 {
            margin-bottom: 10px;
            font-size: 24px;
        }

        .header p {
            opacity: 0.9;
            font-size: 14px;
        }

        .content {
            display: flex;
            min-height: 500px;
        }

        .config-panel {
            flex: 1;
            background: #f8f9fa;
            padding: 20px;
            border-right: 1px solid #e9ecef;
        }

        .form-panel {
            flex: 1;
            padding: 20px;
        }

        .config-item {
            background: white;
            border: 1px solid #dee2e6;
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 15px;
            cursor: pointer;
            transition: all 0.2s;
        }

        .config-item:hover {
            border-color: #4facfe;
            box-shadow: 0 2px 8px rgba(79, 172, 254, 0.2);
        }

        .config-item.active {
            border-color: #4facfe;
            background: #f0f8ff;
        }

        .config-title {
            font-weight: 600;
            color: #495057;
            margin-bottom: 5px;
        }

        .config-type {
            background: #e9ecef;
            color: #6c757d;
            padding: 2px 8px;
            border-radius: 4px;
            font-size: 12px;
            display: inline-block;
            margin-bottom: 8px;
        }

        .config-desc {
            font-size: 14px;
            color: #6c757d;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #495057;
        }

        .required {
            color: #dc3545;
        }

        .form-control {
            width: 100%;
            padding: 12px;
            border: 2px solid #e9ecef;
            border-radius: 8px;
            font-size: 14px;
            transition: border-color 0.2s;
        }

        .form-control:focus {
            outline: none;
            border-color: #4facfe;
            box-shadow: 0 0 0 3px rgba(79, 172, 254, 0.1);
        }

        .file-upload {
            border: 2px dashed #dee2e6;
            border-radius: 8px;
            padding: 20px;
            text-align: center;
            cursor: pointer;
            transition: all 0.2s;
        }

        .file-upload:hover {
            border-color: #4facfe;
            background: #f0f8ff;
        }

        .file-upload input {
            display: none;
        }

        .submit-btn {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            color: white;
            border: none;
            padding: 12px 30px;
            border-radius: 8px;
            font-size: 16px;
            cursor: pointer;
            transition: transform 0.2s;
            width: 100%;
        }

        .submit-btn:hover {
            transform: translateY(-2px);
        }

        .output {
            background: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 8px;
            padding: 15px;
            margin-top: 20px;
            font-family: 'Courier New', monospace;
            font-size: 12px;
            max-height: 200px;
            overflow-y: auto;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="header">
            <h1>🚀 VariableConfig 动态表单生成器</h1>
            <p>基于VariableConfig配置自动生成用户输入表单</p>
        </div>

        <div class="content">
            <!-- 左侧：配置展示 -->
            <div class="config-panel">
                <h3 style="margin-bottom: 20px; color: #495057;">📋 变量配置</h3>
                <div id="configList"></div>
            </div>

            <!-- 右侧：动态表单 -->
            <div class="form-panel">
                <h3 style="margin-bottom: 20px; color: #495057;">📝 用户输入表单</h3>
                <form id="dynamicForm">
                    <div id="formFields"></div>
                    <button type="submit" class="submit-btn">🚀 启动工作流</button>
                </form>

                <div id="output" class="output" style="display: none;">
                    <h4>📤 提交数据：</h4>
                    <pre id="outputContent"></pre>
                </div>
            </div>
        </div>
    </div>

    <script>
        // 模拟VariableConfig配置数据
        const variableConfigs = {
            "user_name": {
                "type": "text",
                "label": "用户姓名",
                "required": true,
                "defaultValue": "",
                "options": {}
            },
            "age": {
                "type": "number",
                "label": "年龄",
                "required": false,
                "defaultValue": 25,
                "options": {
                    "min": 1,
                    "max": 120
                }
            },
            "department": {
                "type": "select",
                "label": "所属部门",
                "required": true,
                "defaultValue": "技术部",
                "options": {
                    "options": ["技术部", "市场部", "运营部", "人事部", "财务部"]
                }
            },
            "document": {
                "type": "file",
                "label": "上传文档",
                "required": false,
                "defaultValue": null,
                "options": {
                    "allowed_types": ["pdf", "docx", "txt"],
                    "max_size": "10MB"
                }
            },
            "description": {
                "type": "textarea",
                "label": "项目描述",
                "required": false,
                "defaultValue": "",
                "options": {
                    "rows": 4,
                    "placeholder": "请输入项目的详细描述..."
                }
            },
            "priority": {
                "type": "select",
                "label": "优先级",
                "required": true,
                "defaultValue": "普通",
                "options": {
                    "options": ["低", "普通", "高", "紧急"]
                }
            }
        };

        // 渲染配置列表
        function renderConfigList() {
            const configList = document.getElementById('configList');
            configList.innerHTML = '';

            Object.entries(variableConfigs).forEach(([key, config]) => {
                const configItem = document.createElement('div');
                configItem.className = 'config-item';
                configItem.innerHTML = `
                    <div class="config-type">${config.type}</div>
                    <div class="config-title">${config.label}</div>
                    <div class="config-desc">
                        键名: ${key} ${config.required ? '<span style="color: #dc3545;">*必填</span>' : ''}
                        ${config.defaultValue ? `<br>默认值: ${config.defaultValue}` : ''}
                    </div>
                `;
                configList.appendChild(configItem);
            });
        }

        // 动态生成表单字段
        function generateFormFields() {
            const formFields = document.getElementById('formFields');
            formFields.innerHTML = '';

            Object.entries(variableConfigs).forEach(([key, config]) => {
                const fieldGroup = document.createElement('div');
                fieldGroup.className = 'form-group';

                const label = document.createElement('label');
                label.className = 'form-label';
                label.innerHTML = `${config.label} ${config.required ? '<span class="required">*</span>' : ''}`;
                fieldGroup.appendChild(label);

                let input;

                switch (config.type) {
                    case 'text':
                        input = document.createElement('input');
                        input.type = 'text';
                        input.className = 'form-control';
                        input.name = key;
                        input.required = config.required;
                        input.value = config.defaultValue || '';
                        break;

                    case 'number':
                        input = document.createElement('input');
                        input.type = 'number';
                        input.className = 'form-control';
                        input.name = key;
                        input.required = config.required;
                        input.value = config.defaultValue || '';
                        if (config.options.min) input.min = config.options.min;
                        if (config.options.max) input.max = config.options.max;
                        break;

                    case 'select':
                        input = document.createElement('select');
                        input.className = 'form-control';
                        input.name = key;
                        input.required = config.required;

                        config.options.options.forEach(option => {
                            const optionEl = document.createElement('option');
                            optionEl.value = option;
                            optionEl.textContent = option;
                            if (option === config.defaultValue) {
                                optionEl.selected = true;
                            }
                            input.appendChild(optionEl);
                        });
                        break;

                    case 'textarea':
                        input = document.createElement('textarea');
                        input.className = 'form-control';
                        input.name = key;
                        input.required = config.required;
                        input.value = config.defaultValue || '';
                        input.rows = config.options.rows || 3;
                        input.placeholder = config.options.placeholder || '';
                        break;

                    case 'file':
                        const fileWrapper = document.createElement('div');
                        fileWrapper.className = 'file-upload';
                        fileWrapper.innerHTML = `
                            <input type="file" name="${key}" ${config.required ? 'required' : ''} 
                                   accept=".${config.options.allowed_types.join(',.')}" />
                            <div>📎 点击上传文件</div>
                            <div style="font-size: 12px; color: #6c757d; margin-top: 8px;">
                                支持格式: ${config.options.allowed_types.join(', ')} 
                                (最大 ${config.options.max_size})
                            </div>
                        `;

                        input = fileWrapper.querySelector('input');
                        fileWrapper.addEventListener('click', () => input.click());
                        input.addEventListener('change', (e) => {
                            const fileName = e.target.files[0]?.name || '点击上传文件';
                            fileWrapper.querySelector('div').textContent = `📎 ${fileName}`;
                        });

                        fieldGroup.appendChild(label);
                        fieldGroup.appendChild(fileWrapper);
                        formFields.appendChild(fieldGroup);
                        return;
                }

                fieldGroup.appendChild(input);
                formFields.appendChild(fieldGroup);
            });
        }

        // 处理表单提交
        document.getElementById('dynamicForm').addEventListener('submit', (e) => {
            e.preventDefault();

            const formData = new FormData(e.target);
            const result = {};

            for (let [key, value] of formData.entries()) {
                if (variableConfigs[key].type === 'number') {
                    result[key] = Number(value);
                } else if (variableConfigs[key].type === 'file') {
                    result[key] = value.name || null;
                } else {
                    result[key] = value;
                }
            }

            // 显示结果
            const output = document.getElementById('output');
            const outputContent = document.getElementById('outputContent');
            outputContent.textContent = JSON.stringify(result, null, 2);
            output.style.display = 'block';

            // 模拟工作流启动
            setTimeout(() => {
                alert('🎉 工作流启动成功！\n\n提交的数据已发送到后端处理。');
            }, 500);
        });

        // 初始化页面
        renderConfigList();
        generateFormFields();
    </script>
</body>

</html>